package net.bac.sbe.web.report.sonarfiltered;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import net.bac.sbe.web.report.ReportFormat;
import net.bac.sbe.web.report.ReportGenerator;
import net.bac.sbe.web.report.ReportManager;
import net.sf.jasperreports.engine.JRDataSource;

public class SonarFilteredReportGenerator extends ReportGenerator {

	private static final String REPORT_DATASOURCE_KEY = "INDICATORS_DS";




	public void generateReport(HashMap<String, Object> reportParameters){
		try {
			String projectName = (String)reportParameters.get(PROJECT_NAME_KEY);
			String projectId = (String)reportParameters.get(PROJECT_ID_KEY);
			String projectAnalyst = (String)reportParameters.get(ANALYST_NAME_KEY);
			Map<String, List<Object>> primaryDataSet = (Map<String, List<Object>>)reportParameters.get(REPORT_DATA_SET_1);
			Map<String, List<Object>> summaryDataSet = (Map<String, List<Object>>)reportParameters.get(REPORT_DATA_SET_2);
			ReportFormat reportFormat = (ReportFormat)reportParameters.get(REPORT_FORMAT_KEY);
			String reportAbsolutePath = (String)reportParameters.get(REPORT_ABSOLUTE_PATH_KEY);
			int dataRows = (Integer)reportParameters.get(ITEM_QUANTITY_KEY);


			Map<String, Object> parameters = prepareParametersForInvocation(projectName, projectId, projectAnalyst, summaryDataSet, dataRows);
			Method method = ReportManager.class.getMethod(reportFormat.getMethodName(), String.class, String.class, Map.class, JRDataSource.class);
			if(!reportAbsolutePath.endsWith(".".concat(reportFormat.getFileExtension()))){
				reportAbsolutePath = reportAbsolutePath.concat(".").concat(reportFormat.getFileExtension());
			}
			method.invoke(null, getReportTemplate(SONAR_FILTERED_REPORT), reportAbsolutePath, parameters, createDataSourceFromMatrix(primaryDataSet));
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		}
	}

	private Map<String, Object> prepareParametersForInvocation(
			String projectName, String projectId, String projectAnalyst, Map<String, List<Object>> data, int dataRows) {
		JRDataSource ds = createDataSourceFromMatrix(data);

		ResourceBundle labels = ResourceBundle.getBundle("net.bac.sbe.web.report.resources.ReportResources");


		Map<String, Object> parameters = new HashMap<String, Object>();
		  parameters.put(PROJECT_NAME_KEY, projectName);
		  parameters.put(ANALYST_NAME_KEY, projectAnalyst);
		  parameters.put("PROJECT_ID", projectId);
		  parameters.put(REPORT_DATASOURCE_KEY, ds);
		  parameters.put("itemQuantity", String.valueOf(dataRows));
		  parameters.put("REPORT_RESOURCE_BUNDLE", labels);
		return parameters;
	}
}
